﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ModemWrite
{
    public partial class FromEnter : Form
    {
        FormMessageReturn _fromReturn = null;
        public FromEnter(string titel, FormMessageReturn formReturnMsg)
        {
            InitializeComponent();
            this.labelTitel.Text = titel;
            if (formReturnMsg == null) Close();
            _fromReturn = formReturnMsg;
        }

        private void FromEnterMsg_FormClosing(object sender, FormClosingEventArgs e)
        {
            //关闭窗口无效
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;           //取消关闭操作 表现为不关闭窗体  
            }
        }

        private void textBoxEnter_KeyPress(object sender, KeyPressEventArgs e)
        {
            //只能输入16进制数字和回车符号'\r'或者'\n'，MAC地址中的字符只能位0-9,A~Z
            if (e.KeyChar == '\n' || e.KeyChar == '\r')
            {
                e.Handled = true;
                //TestStart();
                if (this.textBoxEnter.Text.Trim().Length != 12)
                {
                    MessageBox.Show("输入的不为12位数");
                    this.textBoxEnter.Clear();
                    this.textBoxEnter.Focus();
                }
                else
                {
                    _fromReturn.Msg = this.textBoxEnter.Text.Trim();
                    this.Close();
                    this.Dispose();
                }
            }
            else if ((e.KeyChar >= 'a' && e.KeyChar <= 'z')
                || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
                || (e.KeyChar >= '0' && e.KeyChar <= '9')
                || (e.KeyChar == 8))
            {
                //判断是否位16进制字符，将小写全部转为大写
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                MessageBox.Show("只能输入数字和A-Z");
                this.textBoxEnter.Clear();
                this.textBoxEnter.Focus();
            }
        }
    }
}
